﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Budget
{
    public partial class TransactionDetail : Form
    {
        private DataTable dt_expenses;
        public TransactionDetail()
        {
            InitializeComponent();
            AddExpensesToListView();
        }
        public TransactionDetail(DateTime _dt)
        {
            InitializeComponent();
            AddExpensesToListView();
        }
        decimal total = 0;
        private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
        {
            AddExpensesToListView();
        }
        private void AddExpensesToListView()
        {
            total = 0;
            lblSelectTotal.Text = "";
            lv_cat_tot.Clear();
            lv_cat_tot.FullRowSelect = true;
            lv_cat_tot.View = View.Details;
            int fridays = Dates.getWeeksInMonth(dateTimePicker1.Value.Month, dateTimePicker1.Value.Year);
            List<ListViewGroup> lvGroupList = new List<ListViewGroup>();
            lv_cat_tot.Columns.Add("Date", 150, HorizontalAlignment.Left);
            lv_cat_tot.Columns.Add("Desc", 300, HorizontalAlignment.Left);
            lv_cat_tot.Columns.Add("Company", 125, HorizontalAlignment.Left);
            lv_cat_tot.Columns.Add("Location", 125, HorizontalAlignment.Left);
            lv_cat_tot.Columns.Add("Category", 100, HorizontalAlignment.Left);
            lv_cat_tot.Columns.Add("Amount", 150, HorizontalAlignment.Left);


            List<ListViewItem> ExpenseList = new List<ListViewItem>();
            ListViewItem item;
            //ExpenseCat.Group = lvGroupList;
            
            if(!cbCustomDate.Checked)
            {
                dt_expenses = DatabaseQuery.ExecStringProc(
                        StoredProcs.getExpenseForWeekByDate(
                            Dates.getFridayAssociatedWithDate(dateTimePicker1.Value).AddDays(-6).ToString("MM/dd/yyyy"),
                            Dates.getFridayAssociatedWithDate(dateTimePicker1.Value).ToString("MM/dd/yyyy"),
                            txtFilter.Text, cbShowFixed.Checked)
                );
            }
            else
            {
                dt_expenses = DatabaseQuery.ExecStringProc(
                        StoredProcs.getExpenseForWeekByDate(
                            dateTimePicker1.Value.ToString("MM/dd/yyyy"),
                            dateTimePicker2.Value.ToString("MM/dd/yyyy"),
                            txtFilter.Text, cbShowFixed.Checked)
                );
            }

            //loop through each row
            foreach (DataRow dr in dt_expenses.Rows)
            {
                //create a new row, first column is the date
                item = new ListViewItem(Convert.ToDateTime(dr["Date"].ToString()).ToString("dddd MM/dd/yyyy"));
                //add subitems to the row
                item.SubItems.Add(dr["Description"].ToString());
                item.SubItems.Add(dr["Company"].ToString());
                item.SubItems.Add(dr["Location"].ToString());
                item.SubItems.Add(dr["tblCategoryExpense.Category"].ToString());
                item.SubItems.Add(Convert.ToDecimal(dr["Amount"]).ToString(Money.MoneyFormat));
                //store in list, we will add after we are done
                lv_cat_tot.Items.Add(item);
                total += Convert.ToDecimal(dr["Amount"]);
            }

            lblTotalVal.Text = total.ToString();
        }

        private void cbCustomDate_CheckedChanged(object sender, EventArgs e)
        {
            if (cbCustomDate.Checked)
            {
                dateTimePicker2.Visible = true;
                label1.Visible = true;
            }
            else
            {
                dateTimePicker2.Visible = false;
                label1.Visible = false;
            }
            AddExpensesToListView();
        }

        private void dateTimePicker2_ValueChanged(object sender, EventArgs e)
        {
            AddExpensesToListView();
        }

        private void txtFilter_TextChanged(object sender, EventArgs e)
        {
            //if(txtFilter.TextLength < 3)
            //{
            //    return;
            //}
            AddExpensesToListView();
        }

        private void lv_cat_tot_DoubleClick(object sender, EventArgs e)
        {
            int index;// = dt_expenses.Rows[Convert.ToInt32(lv_cat_tot.SelectedIndices[0]["ID"])];
            index = Convert.ToInt32(dt_expenses.Rows[lv_cat_tot.SelectedIndices[0]]["tblExpenses.ID"]);
            //dt_expenses.Rows[
            
            Add_Purchase ap = new Add_Purchase(index, Money.TransactionType.Expense);
            ap.ShowDialog();
            ap.Dispose();
            AddExpensesToListView();
            //RefreshForm();
        }

        private void lv_cat_tot_SelectedIndexChanged(object sender, EventArgs e)
        {
            decimal total = 0;
            int index;
            ListView myLV = (ListView)sender;
            for(int i = 0 ; i < myLV.SelectedItems.Count ; i++)
            {
                index = myLV.SelectedItems[i].Index;
                total += Convert.ToDecimal(dt_expenses.Rows[index]["Amount"]);
            }
            lblSelectTotal.Text = total.ToString(Money.MoneyFormat);
        }

        private void TransactionDetail_Resize(object sender, EventArgs e)
        {
            if (Form1.ActiveForm != null)
            {
                lv_cat_tot.Height = Form1.ActiveForm.Height - 51 - lv_cat_tot.Location.Y;
                lv_cat_tot.Width = Form1.ActiveForm.Width - 40;
            }
        }

        private void cbShowFixed_CheckedChanged(object sender, EventArgs e)
        {
            AddExpensesToListView();
        }
    }
}
